package bankapp.get;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FundTransfer extends Activity
{
	String username;
	TextView user;
	EditText dstacc,amt;
	Button transfer;
	
	//for login table
	private TodoDbAdapter mDbHelper;
	 private Table table = null;
	 private Long mRowId;
	
	 //for transaction table
	private TransAdapter transacthelper;
	 private Transaction transact = null;
	 private Long transactrow;
	 
	 Cursor ctable,ctransact,cnew;
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fundts);
        Bundle bundle=getIntent().getExtras();
		
		username=(String)bundle.getString("user");
		user=(TextView)this.findViewById(R.id.dspuser);
		user.setText("Welcome "+username);
		dstacc=(EditText)this.findViewById(R.id.etdest);
		amt=(EditText)this.findViewById(R.id.etamt);
		
		mDbHelper = new TodoDbAdapter(this);
        mDbHelper.open();
        mRowId = null;
        
        
        transfer=(Button)this.findViewById(R.id.tf);
        
        transfer.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                transfer();
            }

        });
        
        
		
	}
	public void transfer()
	{
		String dest=dstacc.getText().toString();
		int amount=Integer.parseInt(amt.getText().toString());
		ctable=mDbHelper.fetchAllTodos();
		table = new Table();
		
		while(ctable.moveToNext())
		{
			if(ctable.getString(1).equals(username))
			{
				mRowId=ctable.getLong(0);
				table.username=ctable.getString(1);
				table.password=ctable.getString(2);
				table.ssid=ctable.getString(3);
				table.balance=ctable.getInt(4);
				break;
			}
		}
		table.balance=table.balance-amount;
		mDbHelper.updateTodo(mRowId, table);
		
		System.out.println("Balance debited from account "+username);
		cnew=mDbHelper.fetchAllTodos();
		
		while(cnew.moveToNext())
		{
			if(cnew.getString(1).equals(dest))
			{
				mRowId=cnew.getLong(0);
				table.username=cnew.getString(1);
				table.password=cnew.getString(2);
				table.ssid=cnew.getString(3);
				table.balance=cnew.getInt(4);
				break;
			}
		}
		table.balance=table.balance+amount;
		mDbHelper.updateTodo(mRowId, table);
		
		System.out.println("Balance Credited into account "+dest);
		
		transacthelper = new TransAdapter(this);
		transacthelper.open();
		transactrow = null;
		
		//transact.sno=null;
		transact=new Transaction();
		
		transact.accid=username;		
		transact.balance=amount;
		transact.desc="debit";
		transacthelper.createTodo(transact);
		System.out.println("Data entered into transaction table for source");
		
		transact.accid=dest;		
		transact.balance=amount;
		transact.desc="credit";
		transacthelper.createTodo(transact);
		System.out.println("Data entered into transaction table for destination");
		
		Toast.makeText(this,"Money Transfered Successfully" , Toast.LENGTH_SHORT).show();
		
		
	}

}
